

<script>
    var id = '<%-ret.navbar._id%>';
    var sid = <%=ret.sid%>;
    var query = [];
    query.push("id="+id);
    query.push("sid="+sid);

    var server = <%-JSON.stringify(ret.server)%>;
    let server_select = $('<select id="sid" name="sid"></select>');
    $("#jqGrid").before(server_select);
    for(let k in server){
        server_select.append("<option value=\""+k+"\">"+server[k]+"</option>")
    }
    $("#sid").val(sid);
    $("#sid").on("change",function(){
        let v = $("#sid").val();
        if(v != sid){
            cosjs.reload("?id="+id+"&sid="+v);
        }
    });
    //let magicButton=$('<button id="magicB" name="magicB">神秘按钮</button>');
    //$("#jqGrid").before(magicButton);
    //$("#magicB").click(changeTimeFormat);


    <%-ret.navbar.script%>


    var option = {
        url : GURL(handle, 'page?'+query.join('&')),
        editurl :GURL(handle,'save?'+query.join('&')),
        multipleSearch : true,
        colModel : <%-JSON.stringify(ret.fieids)%>
    }

    for(let c of option.colModel){
        if(c.formatter){
            c.formatter = strFormatter( c.formatter);
        }
        if(c.editoptions && c.editoptions.value){
            c.editoptions.value = strFormatter(c.editoptions.value);
        }
        if(c.searchoptions && c.searchoptions.value){
            c.searchoptions.value = strFormatter(c.searchoptions.value);
        }
    }

    function changeTimeFormat()
    {
        for(let i=1;i<5;++i)
        {
            let _input=$("#jqg"+i);
            if(_input.attr("name")=="time"||_input.attr("name")=="时间"||_input.attr("name")=="日期"||_input.attr("name")=="date")
            {
                 cosjs.jqgrid.timeMask(_input);
            }
        }
    }




    function jsonView(e){
        let txt = JSON.stringify(e);
        return '<span onclick="showJson()" >'+txt+'</span>';
    }

    function showJson(){
        cosjs.alert($(this).html())
    }

    function strFormatter(s){
        if(typeof s !== 'string'){
            return s;
        }
        else if(s==='time'){
            return cosjs.jqgrid.timeFormat;
        }
        else if(s==='json'){
            return JSON.stringify;
        }
        else if(s==='jsonView'){
            return jsonView;
        }
        else if(s.substr(0,1) === '$'){
            let k = s.substr(1);
            eval(`var _f = ${k}`);
            return _f;
        }
        else{
            return s;
        }
        
    }


    $(document).ready(function () {
        cosjs_create_jqgrid(option,'',<%-JSON.stringify(ret.jqgrid)%>);
        $("#search_jqGrid").click(changeTimeFormat);
    });
</script>